package org.hitchhike.web.front.controller;

import javax.servlet.http.HttpServletRequest;

import org.hitchhike.common.domain.BaseEntity;
import org.hitchhike.common.web.controller.BaseController;
import org.hitchhike.core.domain.OrderDetail;
import org.hitchhike.core.domain.User;
import org.hitchhike.core.exception.BaseException;
import org.hitchhike.core.exception.ErrorInfo;
import org.hitchhike.core.form.BaseForm;
import org.hitchhike.core.service.OrderDetailService;
import org.hitchhike.core.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping(value = "/front")
public class TicketController extends BaseController<BaseEntity, Long> {

  @Autowired
  private UserService userService;
  @Autowired
  private OrderDetailService orderDetailService;

  @RequestMapping(value = "/ticket", method = RequestMethod.GET)
  public String ticket(Model model, Integer page) {
    User user = userService
        .findByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
    // Page<OrderDetail> orderDetails = orderDetailService.findPageByUser(page, user.getId());
    // model.addAttribute("orderDetails", orderDetails);
    model.addAttribute("ticket", "active");
    return viewName("ticket");
  }

  @RequestMapping(value = "/ticketA/{type}/{id}", method = RequestMethod.GET)
  public String reserveA(Model model, @PathVariable String type, @PathVariable Long id) {
    OrderDetail orderDetail = orderDetailService.get(id);
    if (orderDetail == null) {
      throw new BaseException("没找到您查询的订单...");
    }
    model.addAttribute("order", orderDetail.getOrder());
    model.addAttribute("orderDetail", orderDetail);
    model.addAttribute("type", type);
    return viewName("reserve");
  }

  @RequestMapping(value = "/ticket/delete/{id}", method = RequestMethod.GET)
  public String delete(RedirectAttributes redirectAttributes, @PathVariable Long id) {
    ErrorInfo<String> r = new ErrorInfo<>();
    try {
      orderDetailService.delete(id);
      r.setCode(ErrorInfo.OK);
      // r.setMessage(ErrorInfo.DELETE_OK);
    } catch (Exception e) {
      r.setCode(ErrorInfo.ERROR);
      // r.setMessage(ErrorInfo.DELETE_ERROR);
    }
    redirectAttributes.addFlashAttribute("r", r);
    return redirectToUrl(viewName("ticket"));
  }

  @RequestMapping(value = "/ticket/update", method = RequestMethod.POST)
  public String updateOrderDetail(BaseForm baseForm, HttpServletRequest request) {
    OrderDetail orderDetail = orderDetailService.get(baseForm.getId());
    if (orderDetail == null) {
      throw new BaseException("没找到您查询的订单...");
    }

    try {
      orderDetailService.update(orderDetail);
    } catch (Exception e) {
      throw new BaseException(e.getMessage());
    }

    return redirectToUrl(viewName("ticket"));
  }

}
